const TOKEN_KEY = "wyt_token_key";
const USER_KEY = "my_user_info_key";
const checkWindow = () => {
  if (typeof window === "undefined") {
    return false;
  } else {
    return true;
  }
};

export const getToken = function () {
  if (!checkWindow()) return;
  const token = window.sessionStorage.getItem(TOKEN_KEY);
  return token;
};
export const setToken = function (token) {
  if (!checkWindow()) return;
  window && window.sessionStorage.setItem(TOKEN_KEY, token);
};
export const removeToken = function () {
  if (!checkWindow()) return;
  window && window.sessionStorage.removeItem(TOKEN_KEY);
};

export const saveUser = function (user: IUser) {
  if (!checkWindow()) return;
  window && window.sessionStorage.setItem(USER_KEY, JSON.stringify(user));
};
export const getUser = function () {
  if (!checkWindow()) return;
  const user = window && window?.sessionStorage?.getItem(USER_KEY);
  return JSON.parse(user);
};
export const removeUser = function () {
  if (!checkWindow()) return;
  window && window.sessionStorage.removeItem(USER_KEY);
};

export const clearAuth = function () {
  if (!checkWindow()) return;
  removeToken();
  removeUser();
};
